# preprocess in demura
import cv2
from datetime import datetime
import numpy as np
import warnings
import pickle

from uitls import *
from lib import *

# main
warnings.filterwarnings('ignore')

if __name__ == '__main__':
# 1.inital var
    logger = inital_var._inital_log()
    logger.info('************<pre_process>开始************')

# 2.param load
    logger.info('<参数读取>开始'); s_t = datetime.now()
    cfg = load_cfg._load_cfg('.//pre_param.ini')
    f_time_load_param = datetime.now() - s_t;logger.info('<参数读取>结束'+',cost time: '+str(f_time_load_param))
    # inital brt
    brt = inital_var._inital_brt(cfg) # used for demura
    brt_aoi = inital_var._inital_brt_aoi(cfg) # used for aoi

#
# # 3.location
#     logger.info('<定位>开始'); s_t = datetime.now()
#     brt = location._location(cfg, brt)
#     f_time_location = datetime.now() - s_t;logger.info('<定位>结束'+',cost time: '+str(f_time_location))
#
# # 4.get_brt
#     logger.info('<亮度获取>开始'); s_t = datetime.now()
#     for color_idx in brt.color:
#         for gray_idx in range(len(brt.gray_level)):
#             s_t0 = datetime.now()
#             img = cv2.imread(cfg['Database']['data_path']+'//'+color_idx+str(brt.gray_level[gray_idx])+'.tif', cv2.IMREAD_GRAYSCALE)
#             img = np.rot90(img, k=int(cfg['Database']['position']) - 1)
#             # img, maps = pre_post._img_crop(img, brt['map' + color_idx])
#             brt_data = get_brt._get_brtJIT(img, brt['map'+color_idx], brt['kernel'+color_idx])
#             brt[color_idx][..., gray_idx] = brt_data
#             logger.info('<Get brt>'+color_idx+str(brt.gray_level[gray_idx]) + ',cost time: ' + str(datetime.now()-s_t0))
#             print('Get brt: '+color_idx+str(brt.gray_level[gray_idx]))
#     f_time_get_brt = datetime.now() - s_t;logger.info('<亮度获取>结束' + ',cost time: ' + str(f_time_get_brt))
#
# # 5.pre_post process
#     logger.info('<前处理-后端处理>开始'); s_t = datetime.now()
#     brt = pre_post._pre_post(cfg, brt)
#     f_time_pre_post = datetime.now() - s_t; logger.info('<前处理-后端处理>结束' + ',cost time: ' + str(f_time_pre_post))

    with open(r'F:\DATA\DATASETS\5\MAT_\brt.pkl', 'rb') as f:
        brt = pickle.load(f)
# 6.AOI dect pre process on img (panel data 1/2 k to 512*512 or 640*640)
    logger.info('<AOI数据预处理>开始'); s_t = datetime.now()
    brt_aoi = pre_aoi.brt2img(brt, brt_aoi)
    f_time_pre_post = datetime.now() - s_t
    logger.info('<AOI数据预处理>结束' + ',cost time: ' + str(f_time_pre_post))

# 7.dect-1 traditional

# 8.dect-2 deep learning
    logger.info('<AOI检测>开始');s_t = datetime.now()
    brt_aoi = dect_aoi.dect(cfg, brt_aoi)
    f_time_pre_post = datetime.now() - s_t
    logger.info('<AOI检测>结束' + ',cost time: ' + str(f_time_pre_post))

# 7.saving
    logger.info('<保存数据>开始'); s_t = datetime.now()
    save_data._save_data(cfg, brt)
    f_time_saving = datetime.now() - s_t;logger.info('<保存数据>结束' + ',cost time: ' + str(f_time_saving))

    tt = f_time_load_param + f_time_location + f_time_get_brt + f_time_pre_post + f_time_saving
    print('DeMura pre-process done \ntotal time: ' + str(tt) + '\nload_param:' + str(f_time_load_param) + '\nlocation:' + str(f_time_location)
          + '\nget_brt:' + str(f_time_get_brt) + '\npre_post:' + str(f_time_pre_post) + '\nsaving:' + str(f_time_saving))
